package com.qf.j2112.controller;

import com.qf.j2112.anno.JWTAnno;
import com.qf.j2112.po.SysEmp;
import com.qf.j2112.service.SysEmpService;
import com.qf.j2112.utils.JwtUtil;
import com.qf.j2112.vo.ActionResultVO;
import com.qf.j2112.vo.LogInVO;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api")
@CrossOrigin
public class SysEmpController {

    @Resource
    private SysEmpService sysEmpService;

    //后台管理员登录
    @JWTAnno(name = "管理员登录")
    @PostMapping("/admin/login")
    public ActionResultVO adminlogin(@RequestBody LogInVO logInVO){
        ActionResultVO vo=null;
        SysEmp sysEmp=sysEmpService.findEmpByEname(logInVO.getUserName());
        if(sysEmp.getStateCode()!=1){
            vo=new ActionResultVO(-100,"账户"+sysEmp.getEname()+"账户已于"+sysEmp.getCreateTime().toLocaleString()+"停用",null);
        }else if (!ObjectUtils.isEmpty(sysEmp)&&sysEmp.getPassword().equals(logInVO.getPassword())){
            String token= JwtUtil.getToken(logInVO.getUserName());
            vo=new ActionResultVO(200,"登录成功",token);
        } else {
            System.out.println("用户名或密码错误");
            vo=new ActionResultVO(-200,"登录失败,用户名或密码错误",null);
        }
        return vo;
    }


    /*
    //以下后台增删查改
    */
    //获取全部员工列表
    @JWTAnno(name = "获取全部员工列表")
    @GetMapping("/emps")
    public ActionResultVO getEmpList(){
        ActionResultVO vo=null;
        List<SysEmp> empList = sysEmpService.findAll();
        if (!ObjectUtils.isEmpty(empList)){
            vo=new ActionResultVO(200,"查询全部员工成功",empList);
        }else {
            vo=new ActionResultVO(-200,"查询全部员工失败",null);
        }
        return vo;
    }

    //获取单个用户的信息
    @JWTAnno(name = "获取单员工的信息")
    @GetMapping("/emp")
    public ActionResultVO getOneEmp(int eid){
        ActionResultVO vo=null;
        SysEmp emp=sysEmpService.findEmpByEid(eid);
        if (!ObjectUtils.isEmpty(emp)){
            vo=new ActionResultVO(200,"查询单个员工成功",emp);
        }else {
            vo=new ActionResultVO(-200,"查询单个员工失败",null);
        }
        return vo;
    }

    @PostMapping("/addEmp")
    @JWTAnno(name = "新增员工信息")
    public ActionResultVO addUser(@RequestBody SysEmp sysEmp){
        ActionResultVO vo=null;
        //String spass= new SimpleHash("MD5",sysUser.getPassword(),sysUser.getLoginName(),1024).toString();
        //sysUser.setPassword(spass);
        sysEmp.setCreateTime(new Date());
        System.out.println("添加前："+sysEmp);
        SysEmp emp=sysEmpService.addSysEmp(sysEmp);
        System.out.println("添加后："+sysEmp);
        if(!ObjectUtils.isEmpty(sysEmp)){
            vo=new ActionResultVO(200,"添加员工成功",sysEmp);
        }else {
            vo=new ActionResultVO(-200,"添加员工失败",null);
        }
        return vo;
    }

    @DeleteMapping("/delEmp")
    @JWTAnno(name = "删除一个员工")
    public ActionResultVO deleteUser(int eid){
        ActionResultVO vo=null;
        if(sysEmpService.deleteSysEmp(eid)){
            vo=new ActionResultVO(200,"删除员工成功",null);
        }else {
            vo=new ActionResultVO(-200,"删除员工失败",null);
        }
        return vo;
    }


    //动态sql
    @PutMapping ("/updateEmp")
    @JWTAnno(name = "修改一个员工的信息")
    public ActionResultVO updateUser(@RequestBody SysEmp sysEmp){
        sysEmp.setCreateTime(new Date());
        ActionResultVO vo=null;
        if(sysEmpService.updateSysEmp(sysEmp)){
            vo=new ActionResultVO(200,"修改用户成功",null);
        }else {
            vo=new ActionResultVO(-200,"修改用户失败",null);
        }
        return vo;
    }

}
